package com.liruan.rest.dao.lang;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;

public class SelectInDriver extends BaseDriver
{
	private final Pattern SELECT_IN_PATTERN = Pattern.compile("\\(#\\{(\\w+)\\}\\)");

	@Override
	public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType)
	{
		Matcher matcher = SELECT_IN_PATTERN.matcher(script);
		if (matcher.find())
		{
			script = matcher.replaceAll("(<foreach collection=\"$1\" item=\"__item\" separator=\",\" >#{__item}</foreach>)");
		}

		return super.createSqlSource(configuration, script, parameterType);
	}
}
